/**
* ...
* @author Default
* @version 0.1
*/

package bdz.common.display{
	import bdz.common.display.DisplayLoader;
	import bdz.math.trigo;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.*;
	import bdz.common.utils.TweenLite;
	import fl.transitions.easing.*;
	
	public class ThumbToLarge extends DisplayLoader {
		
		private var frontLoader:bdzLoader;
		private var defDimension:Object;
		private var frontDimension:Object;
		private var defImage:String;
		private var frontImage:String;
		private var _centered:Boolean = true;
		private var rspeed:Number = .3;
		private var ease:Function = Strong.easeInOut;
		function ThumbToLarge(def:String = null):void{
			super(def);
			addEventListener(Event.UNLOAD, cleanMeUp);
		}
		override protected function initializeComponent():void{
			super.initializeComponent();
		}
		private function moveToCenter(targ:DisplayObject):void{
			try{
				targ.x = -defDimension.width/2;
				targ.y = -defDimension.height/2;
			}catch(e:Error){
				
			}
		}
		
		private function moveToStartPoint(targ:DisplayObject):void{
			try{
				targ.x = 0;
				targ.y = 0;
			}catch(e:Error){
				
			}
		}
		override protected function completeHandler(evt:Event):void{			
			frontLoader = new bdzImage();
			frontLoader.visible = false;
			configureListener(frontLoader.holder.contentLoaderInfo);
			if(_centered){
				moveToCenter(frontLoader);
			}
			moveToCenter(holder);
			//width = height = 0;
			//TweenLite.to(this, rspeed, {width:defDimension.width,height:defDimension.height,ease:ease}, 0)
			dispatchEvent(evt);
		}
		override protected function progressHandler(evt:ProgressEvent):void{
			
		}
		protected function frontCompleteHandler(evt:Event):void{
			frontLoader.width = defDimension.width;
			frontLoader.height = defDimension.height;
			addChild(frontLoader);
			showLargeImage();
		}
		protected function frontHttpStatusHandler(evt:HTTPStatusEvent):void{
			
		}
		protected function frontInitHandler(evt:Event):void{
			
		}
		protected function frontIoErrorHandler(evt:IOErrorEvent):void{
			
		}
		protected function frontOpenHandler(evt:Event):void{
			
		}
		protected function frontProgressHandler(evt:ProgressEvent):void{
			
		}
		protected function frontUnLoadHandler(evt:Event):void{
			
		}
		private function cleanMeUp(evt:Event):void{
			var dispatcher:IEventDispatcher = frontLoader.holder.contentLoaderInfo as IEventDispatcher;
			dispatcher.removeEventListener(Event.COMPLETE, frontCompleteHandler);
            dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, frontHttpStatusHandler);
            dispatcher.removeEventListener(Event.INIT, frontInitHandler);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, frontIoErrorHandler);
            dispatcher.removeEventListener(Event.OPEN, frontOpenHandler);
            dispatcher.removeEventListener(ProgressEvent.PROGRESS, frontProgressHandler);
            dispatcher.removeEventListener(Event.UNLOAD, frontUnLoadHandler);
		}
		private function configureListener(dispatcher:IEventDispatcher):void{
			dispatcher.addEventListener(Event.COMPLETE, frontCompleteHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, frontHttpStatusHandler);
            dispatcher.addEventListener(Event.INIT, frontInitHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, frontIoErrorHandler);
            dispatcher.addEventListener(Event.OPEN, frontOpenHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, frontProgressHandler);
            dispatcher.addEventListener(Event.UNLOAD, frontUnLoadHandler);
		}
		private function setDefaultDimensions():void{
			imgWidth = defDimension.width;
			imgHeight = defDimension.height;
			
		}
		private function startLoadingLarge():void{
			frontLoader.source = frontImage;
		}
		private function showLargeImage():void{
			frontLoader.alpha = 0;
			frontLoader.visible = true;
			TweenLite.to(frontLoader, rspeed, {alpha:1, ease:ease}, 0)			
		}
		public function startLoad():void{
			super.source = defImage;
			
		}
		
		public function loadLargeImage():void{
			//TweenLite.to(this, rspeed, {width:frontDimension.width,height:frontDimension.height, ease:ease}, 0, startLoadingLarge)
			startLoadingLarge();
			
		}
		override public function set source(s:String):void{
			defImage = s;
			startLoad();
		}
		public function showLarge():void{
			
		}
		public function set default_dimensions(d:Object):void{
			defDimension = d;
			setDefaultDimensions();
		}
		public function get default_dimensions():Object{
			return defDimension;
		}
		public function set large_dimensions(d:Object){
			frontDimension = d;
		}
		public function get large_dimensions():Object{
			return frontDimension;
		}
		public function set default_image(d:String):void{
			defImage = d;
		}
		public function get default_image():String{
			return defImage;
		}
		public function set front_image(d:String):void{
			frontImage = d;
		}
		public function get front_image():String{
			return frontImage;
		}
		public function set centered(c:Boolean):void{
			_centered = c;
			if(c){
				moveToCenter(holder);
			}else{
				moveToStartPoint(holder);
			}
		}
	}
	
}